10378. Unexpressed

 

One integer n is given. How many integers between 1 and n (inclusive) are unrepresentable as ab, where a and b are integers not less than 2?

 

Input. One positive integer n (1 ≤ n ≤ 1010).

 

Output. Print the amount of unrepresentable numbers.

 

Sample input 1

Sample output 1

8

6

 

 

Sample input 2

Sample output 2

100000

99634

 

 

SOLUTION

data structures - set

 

Algorithm analysis

The number of non-representable numbers is

n – the amount of numbers representable in the form ab

Find all numbers representable as ab, where a > 1, b > 1, abn. There are not many such numbers, so all of them can be generated and stored in a container. The powers can contain repetitions, for example 212, 46 and 163. Duplicate numbers should be removed, so well use set as a container.

Generate the powers ab, where a = 2, 3, 4,…, . For each value of a, iterate over b = 2, 3, 4, ... until the power is not greater than n. For example:

·        powers of two 22, 23, 24, … n;

·        powers of three 32, 33, 34, … n;

·        powers of four 42, 43, 44, … n;

 

Example

In the interval [1; 8] there are two numbers representable as powers: 22 = 4 and 23 = 8. Thus, there will be 8 – 2 = 6 unrepresentable numbers.

Let n = 50. Then the following powers will be generated:

·        22 = 4, 23 = 8, 24 = 16, 25 = 32 (26 = 64 > 50);

·        32 = 9, 33 = 27 (34 = 81 > 50);

·        42 = 16 (43 = 64 > 50);

·        52 = 25 (53 = 125 > 50);

·        62 = 36 (63 = 216 > 50);

·        72 = 49 (73 = 343 > 50);

Note that 24 = 16 and 42 = 16, however, duplicates in the set will be deleted. For n = 50, the set s will have the form: {4, 8, 9, 16, 25, 27, 32, 36, 49}. The number of unrepresentable numbers in the interval [1; 50] is 50 – s.size() = 50 – 9 = 41.

 

Algorithm realization

Declare a set s.

 

#define ll long long

set<ll> s;

 

Read the value of n.

 

scanf("%lld", &n);

 

Iterate through the base of the power a.

 

for (a = 2; a * a <= n; a++)

{

 

Initialize x = a2. Then in the variable x iterate over the powers of number a: a3, a4, … . Insert the generated powers into the set s.

 

  ll x = a * a;

  while (x <= n)

  {

    s.insert(x);

    x *= a;

  }

}

 

The value of s.size() contains the amount of numbers that can be represented as ab. Print the answer ns.size().

 

printf("%lld\n", n - s.size());

 

Java realization

 

import java.util.*;

 

class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    TreeSet<Long> s = new TreeSet<Long>();

    long n = con.nextLong();

   

    for (long a = 2; a * a <= n; a++)

    {

      long x = a * a;

      while (x <= n)

      {

        s.add(x);

        x *= a;

      }

    }

    System.out.print(n - s.size());

    con.close();

  }

}